﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Marbles.Core.Content.Sites;
using Marbles.Core.Connectors;
using System.Data;

namespace Marbles.Core.Content.Profiles
{
    public class ProfileQuestionCollection : ICollection<ProfileQuestion>
    {
        public int Count
        {
            get { return items.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public ProfileQuestion this[int index] { get { return items[index]; } set { items[index] = value; } }
        public string SiteId { get { return _siteid; } }

        private List<ProfileQuestion> items = null;
        private string _siteid = string.Empty;

        public ProfileQuestionCollection()
        {
            items = new List<ProfileQuestion>();
            _siteid = SiteDefinition.GetCurrent().SiteId;
            Load();
        }

        public ProfileQuestionCollection(string Siteid)
        {
            items = new List<ProfileQuestion>();
            _siteid = Siteid;
            Load();
        }

        public void Load()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT * FROM profiles WHERE siteid = @siteid ORDER BY questionid ASC");
            cmd.AddParameter("@siteid", _siteid);
            items.Clear();
            foreach (DataRow dr in dbc.FetchDataTable(cmd).Rows)
            {
                items.Add(new ProfileQuestion(dr));
            }
        }


        public void Save()
        {
            foreach (ProfileQuestion p in items)
            {
                p.Update();
            }
        }

        public void Delete(ProfileQuestion Question)
        {
            Question.Delete();
        }

        public void Add(ProfileQuestion item)
        {
            items.Add(item);
        }

        public void Clear()
        {
            items.Clear();
        }

        public bool Contains(ProfileQuestion item)
        {
            return items.Contains(item);
        }

        public void CopyTo(ProfileQuestion[] array, int arrayIndex)
        {
            items.CopyTo(array, arrayIndex);
        }

        public bool Remove(ProfileQuestion item)
        {
            item.Delete();
            return items.Remove(item);
        }

        public IEnumerator<ProfileQuestion> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return items.GetEnumerator();
        }

        public static Dictionary<Int32, string> GetProfileQuestionTypes()
        {
            Dictionary<Int32, string> items = new Dictionary<int, string>();
            items.Add(5, "Calendar Date");
            items.Add(3, "Checkbox");
            items.Add(7, "Image Upload");
            items.Add(4, "Multi Line Textbox");
            items.Add(1, "Single Line Textbox");
            items.Add(6, "Year Dropdown");
            items.Add(2, "Yes/No Dropdown");
            return items;
        }
    }

    public enum ProfileQuestionType
    {
        Text = 1,
        YesNo = 2,
        YesNoChk = 3,
        TextMulti = 4,
        Date = 5,
        Year = 6,
        ImageUpload = 7
    }
}